package com.efast.dmom.mgmt.service.mes.impl;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.MesPlanScheduleInfo;
import com.efast.dmom.mgmt.bean.mes.MesWorkCalendar;
import com.efast.dmom.mgmt.bean.mes.TmShift;
import com.efast.dmom.mgmt.bean.mes.TmShiftDetail;
import com.efast.dmom.mgmt.dao.mes.MesWorkCalendarDao;
import com.efast.dmom.mgmt.entity.master.TmBasWorkshop;
import com.efast.dmom.mgmt.service.mes.MesWorkCalendarService;
import com.efast.dmom.mgmt.utils.Assist;
@Service
public class MesWorkCalendarServiceImpl implements MesWorkCalendarService{

	@Autowired
	private MesWorkCalendarDao mesWorkCalendarDao;

	@Override
	public ResultBean getAllworkShop(Map<String,Object> map) {
		ResultBean resultBean = new ResultBean();
		List<TmBasWorkshop> allworkShop = null;
		Object plantNo = map.get("plant");
		if (plantNo != null && plantNo != "") {
			String plantNo1 = (String)plantNo;
			allworkShop= mesWorkCalendarDao.getAllworkShop(plantNo1);
		}
		if (allworkShop != null && allworkShop.size()>0) {
			resultBean.setSuccess(true);
			resultBean.setObject(allworkShop);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("查询不到任何车间！");
		}
		
		return resultBean;
	}

	@Override
	public ResultBean getworkCanlendarByMonth(Map<String,Object> map) {
		ResultBean resultBean = new ResultBean();
		String startDate = (String)map.get("startDate");
		String endDate = (String)map.get("endDate");
		String workshop = (String)map.get("workshop");
		Assist assist = new Assist();
		assist.setRequires(Assist.andGte("calendar_date", startDate));
		assist.setRequires(Assist.andLte("calendar_date", endDate));
		assist.setRequires(Assist.andEq("workshop", workshop));
		assist.setRequires(Assist.andEq("is_deleted", 0));
		List<MesWorkCalendar> selectMesWorkCalendar = mesWorkCalendarDao.selectMesWorkCalendar(assist);
		if (selectMesWorkCalendar.size()>0) {
			resultBean.setSuccess(true);
			resultBean.setObject(selectMesWorkCalendar);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询到数据！");
		}
		
		return resultBean;
	}

	@Override
	public ResultBean addShiftByWorkCanlendars(List<String> calendarDate, MesWorkCalendar mesWorkCalendar) {
		ResultBean resultBean = new ResultBean(true);
		mesWorkCalendar.setCreateTime(new Date());
		mesWorkCalendar.setCreateUser(SpringWebUtils.getRequestUser());
		mesWorkCalendar.setLastUpdateTime(new Date());
		mesWorkCalendar.setLastUpdateUser(SpringWebUtils.getRequestUser());
		mesWorkCalendar.setStatus("Y");
		mesWorkCalendar.setIsDeleted(0);
		mesWorkCalendar.setActivate(0);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("calendarDate", calendarDate);
		map.put("mesWorkCalendar", mesWorkCalendar);
		List<MesWorkCalendar> mesCalendarDate = mesWorkCalendarDao.selectCalendarsBycalendarAndWorkshop(map);
		map.put("mesCalendarDate", mesCalendarDate);
		mesWorkCalendarDao.addShiftByWorkCanlendars(map);
		return resultBean;
	}

	@Override
	public ResultBean updateWorkCanlendar(MesWorkCalendar mesWorkCalendar) {
		ResultBean resultBean = new ResultBean(true,"");
		mesWorkCalendar.setLastUpdateTime(new Date());
		mesWorkCalendar.setLastUpdateUser(SpringWebUtils.getRequestUser());
		Date calendarDate = getDateShort(mesWorkCalendar.getCalendarDate());
		mesWorkCalendar.setCalendarDate(calendarDate);
		Assist assist = new Assist();
		assist.setRequires(Assist.andEq("calendar_date", calendarDate));
		assist.setRequires(Assist.andEq("workshop", mesWorkCalendar.getWorkshop()));
		assist.setRequires(Assist.andEq("is_deleted", 0));
		List<MesWorkCalendar> selectMesWorkCalendar = mesWorkCalendarDao.selectMesWorkCalendar(assist);
		if (selectMesWorkCalendar.size() == 1) {
			mesWorkCalendar.setId(selectMesWorkCalendar.get(0).getId());
			mesWorkCalendarDao.updateMesWorkCalendarByShiftId(mesWorkCalendar);
		}
		
		return resultBean;
	}

	public Date getDateShort(Date date) {
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateString = formatter.format(date);  
	    ParsePosition pos = new ParsePosition(8);  
	    Date currentTime_2 = null;
		try {
			currentTime_2 = formatter.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}  
		return currentTime_2;
		
	}
	@Override
	public ResultBean deleteShiftByCanlendar(Map<String,Object> map) {
		ResultBean resultBean = new ResultBean(true,"");
		@SuppressWarnings("unchecked")
		String calendarDate = (String)map.get("calendarDate");
		Integer shiftId = (Integer)map.get("shiftId");
		String workshop = (String)map.get("workshop");
		Assist assist = new Assist();
		assist.setRequires(Assist.andEq("calendar_date", calendarDate));
		assist.setRequires(Assist.andEq("workshop", workshop));
		assist.setRequires(Assist.andEq("is_deleted", 0));
		List<MesWorkCalendar> selectMesWorkCalendar = mesWorkCalendarDao.selectMesWorkCalendar(assist);
		if (selectMesWorkCalendar.size() == 1) {
			mesWorkCalendarDao.deleteShiftByCanlendar(calendarDate, shiftId, selectMesWorkCalendar.get(0).getId());
		}
		return resultBean;
	}

	@Override
	public ResultBean selectShiftDetailByShiftId(MesWorkCalendar mesWorkCalendar) {
		ResultBean resultBean = new ResultBean();
		mesWorkCalendar.setCalendarDate(getDateShort(mesWorkCalendar.getCalendarDate()));
		List<TmShiftDetail> selectShiftDetailByShiftId = mesWorkCalendarDao.selectShiftDetailByShiftId(mesWorkCalendar);
		if(selectShiftDetailByShiftId.size()>0) {
			resultBean.setSuccess(true);
			resultBean.setObject(selectShiftDetailByShiftId);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查询出班次详细信息！请检查班次配置");
		}
		
		return resultBean;
	}

	@Override
	public ResultBean updateIfwork(Map<String,Object> map) {
		ResultBean resultBean = new ResultBean(true,"");
		String status = (String)map.get("status");
		String workshop = (String)map.get("workshop");
		String factory = (String)map.get("factory");
		Map<String, Object> map1 = new HashMap<String,Object>();
		@SuppressWarnings("unchecked")
		List<String> calendarDate = (List<String>)map.get("calendarDate");
		 MesWorkCalendar mesWorkCalendar = new MesWorkCalendar();
		 mesWorkCalendar.setStatus(status);
		 mesWorkCalendar.setWorkshop(workshop);
		 mesWorkCalendar.setFactoryCode(factory);
		 mesWorkCalendar.setCreateTime(new Date());
		 mesWorkCalendar.setLastUpdateTime(new Date());
		 mesWorkCalendar.setCreateUser(SpringWebUtils.getRequestUser());
		 mesWorkCalendar.setLastUpdateUser(SpringWebUtils.getRequestUser());
		 map1.put("mesWorkCalendar", mesWorkCalendar);
		map1.put("calendarDate", calendarDate);
		mesWorkCalendarDao.deleteMesCalendarByShopAndcalendar(map1);
		Integer addcalendarDatas = mesWorkCalendarDao.addcalendarDatas(map1);
		if (addcalendarDatas != null) {
			resultBean.setSuccess(true);
		} else {
			resultBean.setSuccess(false);
		}
		return resultBean;
	}

	public Date getformatStringTodate(String dateString) {
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); 
	    Date currentTime_2 = null;
		try {
			currentTime_2 = formatter.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}  
		return currentTime_2;
	}
	@Override
	public ResultBean selectShiftIdByCanlendar(Map<String,Object> map) {
		String workshop = (String)map.get("workshop");
		String mesWorkCalendar = (String)map.get("calendarDate");
		ResultBean resultBean = new ResultBean();
		Map<String, Object> map1 = new HashMap<String,Object>();
		map1.put("calendarDate", mesWorkCalendar);
		map1.put("workshop", workshop);
		List<MesWorkCalendar> selectMesWorkCalendar = mesWorkCalendarDao.selectCalendarsByWorkshop(map);
		if (selectMesWorkCalendar.size()>0) {
			 List<TmShift> selectCalendarShift = mesWorkCalendarDao.selectCalendarByRelationId(selectMesWorkCalendar);
			 for (TmShift tmShift : selectCalendarShift) {
				for(int i=0;i<selectMesWorkCalendar.size();i++) {
					if (tmShift.getTmShiftId() == selectMesWorkCalendar.get(i).getShiftId() && tmShift.getModel().equals(selectMesWorkCalendar.get(i).getModel())) {
						tmShift.setMesWorkCalendar(selectMesWorkCalendar.get(i));
					}
				}
			}
			resultBean.setSuccess(true);
			resultBean.setObject(selectCalendarShift);
		} else {
			resultBean.setSuccess(false);
			resultBean.setMessage("无法查找到指定日历班次信息！");
		}
		return resultBean;
	}

	@Override
	public ResultBean queryAllPlant() {
		ResultBean resultBean = new ResultBean(true);
		 List<Map<String, String>> queryAllPlant = mesWorkCalendarDao.queryAllPlant();
		 resultBean.setObject(queryAllPlant);
		return resultBean;
	}

	/**
	 * 用于比较两个date类型的数据是否相等
	 * */
	private  boolean sameDate(Date d1, Date d2) {  
	    LocalDate localDate1 = ZonedDateTime.ofInstant(d1.toInstant(), ZoneId.systemDefault()).toLocalDate();  
	    LocalDate localDate2 = ZonedDateTime.ofInstant(d2.toInstant(), ZoneId.systemDefault()).toLocalDate();  
	    return localDate1.isEqual(localDate2);  
	}  
	/**
	 * 查询日历中悬浮框数据
	 * */
	@Override
	public ResultBean selectCalendarSuspendDate(Map<String, Object> map) {
		ResultBean resultBean = new ResultBean();
		String vinFlag = (String)map.get("vinFlag");
		if (vinFlag != null && vinFlag != "") {
			List<MesWorkCalendar> selectCalendarRelationSumPlaninput = mesWorkCalendarDao.selectCalendarRelationSumPlaninput(map);
			List<MesPlanScheduleInfo> selectCalendarSuspendDate = mesWorkCalendarDao.selectCalendarSuspendDate(map);
			for (MesWorkCalendar mesWorkCalendar : selectCalendarRelationSumPlaninput) {
				for (MesPlanScheduleInfo mesPlanScheduleInfo : selectCalendarSuspendDate) {
					boolean sameDate = sameDate(mesPlanScheduleInfo.getPlanDate(),mesWorkCalendar.getCalendarDate());
					if (sameDate) {
						mesWorkCalendar.setScheduleCount(mesPlanScheduleInfo.getScheduleCount());
						break;
					}
				}
			}
			resultBean.setSuccess(true);
			resultBean.setObject(selectCalendarRelationSumPlaninput);
		} else {
			resultBean.setSuccess(false);
		}
		return resultBean;
	}
	
}
